package com.hzy.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.oms.domain.bo.OrderItemBo;
import com.hzy.oms.domain.vo.OrderItemVo;
import com.hzy.oms.domain.OrderItem;
import com.hzy.oms.mapper.OrderItemMapper;
import com.hzy.oms.service.IOrderItemService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单中所包含的商品Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class OrderItemServiceImpl implements IOrderItemService {

    private final OrderItemMapper baseMapper;

    /**
     * 查询订单中所包含的商品
     */
    @Override
    public OrderItemVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单中所包含的商品列表
     */
    @Override
    public TableDataInfo<OrderItemVo> queryPageList(OrderItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderItem> lqw = buildQueryWrapper(bo);
        Page<OrderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单中所包含的商品列表
     */
    @Override
    public List<OrderItemVo> queryList(OrderItemBo bo) {
        LambdaQueryWrapper<OrderItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderItem> buildQueryWrapper(OrderItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, OrderItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductId() != null, OrderItem::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutProductId()), OrderItem::getOutProductId, bo.getOutProductId());
        lqw.eq(bo.getSkuId() != null, OrderItem::getSkuId, bo.getSkuId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutSkuId()), OrderItem::getOutSkuId, bo.getOutSkuId());
        lqw.eq(bo.getProductSnapshotId() != null, OrderItem::getProductSnapshotId, bo.getProductSnapshotId());
        lqw.eq(bo.getSkuSnapshotId() != null, OrderItem::getSkuSnapshotId, bo.getSkuSnapshotId());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), OrderItem::getPic, bo.getPic());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), OrderItem::getProductName, bo.getProductName());
        lqw.eq(bo.getSalePrice() != null, OrderItem::getSalePrice, bo.getSalePrice());
        lqw.eq(bo.getPurchasePrice() != null, OrderItem::getPurchasePrice, bo.getPurchasePrice());
        lqw.eq(bo.getQuantity() != null, OrderItem::getQuantity, bo.getQuantity());
        lqw.eq(bo.getProductCategoryId() != null, OrderItem::getProductCategoryId, bo.getProductCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getSpData()), OrderItem::getSpData, bo.getSpData());
        return lqw;
    }

    /**
     * 新增订单中所包含的商品
     */
    @Override
    public Boolean insertByBo(OrderItemBo bo) {
        OrderItem add = BeanUtil.toBean(bo, OrderItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单中所包含的商品
     */
    @Override
    public Boolean updateByBo(OrderItemBo bo) {
        OrderItem update = BeanUtil.toBean(bo, OrderItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OrderItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单中所包含的商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
